<template>
  <div style="width: 100%; height: 100%">

    <!-- 反馈列表 -->
    <!-- 表格 -->
    <div style="margin: 10px 0">
      <el-table :data="feedbacks" stripe :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666' }">
        <el-table-column prop="feedback_id" label="反馈ID" width="90" align="center"></el-table-column>
        <el-table-column prop="user_id" label="用户名" width="90" align="center"></el-table-column>
        <el-table-column prop="content" label="内容"></el-table-column>
        <el-table-column prop="submitted_at" label="提交时间" width="200" align="center">
          <template slot-scope="scope">
            {{ formatDate(scope.row.submitted_at) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 'unresolved'">未解决</span>
            <span v-if="scope.row.status === 'in progress'">处理中</span>
            <span v-if="scope.row.status === 'resolved'">已解决</span>
          </template>
        </el-table-column>
        <el-table-column prop="response" label="回复"></el-table-column>
        <el-table-column prop="responded_at" label="回复时间" width="200" align="center">
          <template slot-scope="scope">
            {{ formatDate(scope.row.responded_at) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="handleDelete(scope.row.feedback_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>



    <!-- 新增/编辑账号模态框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%" >
      <el-form :model="editItem" label-width="100px">

        <el-form-item label="内容">
          <el-input type="content"  v-model="editItem.content"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="editItem.status" placeholder="请选择状态">
            <el-option label="未解决" value="unresolved"></el-option>
            <el-option label="处理中" value="in progress"></el-option>
            <el-option label="已解决" value="resolved"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回复">
          <el-input type="response" v-model="editItem.response"></el-input>
        </el-form-item>
        <el-form-item label="回复时间">
          <div class="block">
            <el-date-picker
                v-model="editItem.responded_at"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSaveEdit">{{ isEditing ? '保存' : '新增' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      feedbacks: [], // 反馈信息列表
      editItem: { // 编辑/新增的数据
        feedback_id:'',
        content: '',
        status: '',
        response: '',
        responded_at: '',

      },
      dialogVisible: false ,
      isEditing:false,
    };
  },
  mounted() {
    this.fetchFeedbacks();
  },
  computed: {
    dialogTitle() {
      return this.isEditing ? '编辑信息' : '新增信息'; //false为新增，true为编辑
    }
  },
  methods: {
    formatDate(dateTime) {
      const date = new Date(dateTime);
      return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`;
    },
    closeDialog(){
      this.dialogVisible=false;
    },
    handleSaveEdit(){
        // 编辑状态下发送编辑请求
        axios.put("http://localhost:8080/Feedback/edit", this.editItem)
            .then(response => {
              // 处理编辑成功逻辑
              this.$message.success("编辑成功");
              this.dialogVisible = false;
              this.fetchFeedbacks(); // 刷新数据
            })
            .catch(error => {
              // 处理编辑失败逻辑
              console.error('Error editing item:', error);
              this.$message.error("编辑失败");
            });
    },
    // 获取反馈信息
    fetchFeedbacks() {
      let url = 'http://localhost:8080/Feedback/selectAll';
      let params = {};


      axios.get(url, { params })
          .then(response => {
            this.feedbacks = response.data.data;
          })
          .catch(error => {
            console.error('Error fetching feedbacks:', error);
          });
    },
    reset() {
      this.fetchFeedbacks();
    },
    handleEdit(row) {
      // 将选中行的数据赋值给编辑的数据项
      this.editItem = Object.assign({}, row);
      // 设置编辑状态为true
      this.isEditing = true; // 设置编辑状态为true，表示编辑
      // 打开新增/编辑弹窗
      this.dialogVisible = true;
      console.log('Edit:', row);
    },
    handleDelete(feedback_id) {
      this.$confirm('此操作将永久删除该反馈，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.delete(`http://localhost:8080/Feedback/delete/${feedback_id}`)
            .then(response => {
              this.$message.success('删除成功');
              this.fetchFeedbacks(); // Refresh data
            })
            .catch(error => {
              console.error('Error deleting feedback:', error);
              this.$message.error('删除失败');
            });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    }
  }
};
</script>

<style scoped>
.table-container {
  margin-top: 20px;
}
</style>
